from network import *
from generate import GenerateNetwork
import time



node_num = 60
sd_num = 30
remaining_num = 6

# start_time = time.time()
# GenerateNetwork("./data/generate.txt", node_num, sd_num, .55, β, 1, 80)
 
# print("拓扑txt已生成")

# end_time = time.time()

# total_time = end_time - start_time

# print(f"拓扑txt生成耗时为: {total_time} 秒")

start_time = time.time()
loop_num = 3
time_num = 20

ec_counter_average = 0
for i in range(loop_num):
    print(f"第{i+1}次实验")
    qnetwork = QNetwork("./data/generate.txt", remaining_num, node_num)
    # print("拓扑已生成")
    epList = qnetwork.get_epList() 
    eps_ep_index, eps_ep_nodes = qnetwork.get_eps_ep_list(epList)

    mark = i
    # print(f"start with{qnetwork.elMatrix}")
    # 时隙数
    for i in range(time_num):
        qnetwork.ec_selectNodes.clear()
        eps_sd_pair_step_num_grade_down = {}
        for node in qnetwork.nodeList:
            if node.epsFlag == 1:
                eps_sd_pair_step_num_grade_down[node.nodeID] = qnetwork.get_ep_step(node.nodeID, eps_ep_index, epList)

        # print("开始循环")

        if_change = False  # 假设 False 表示没有变化
        if_change, old_max = qnetwork.updateSelectNodeID(epList, eps_ep_index, eps_ep_nodes, eps_sd_pair_step_num_grade_down)
        for i in range(10):
            if_change, new_old_max = qnetwork.updateSelectNodeID(epList, eps_ep_index, eps_ep_nodes, eps_sd_pair_step_num_grade_down, old_max)
            old_max == new_old_max
            # print(if_change)
            if not if_change:  # 如果 if_change 为 False，则退出内层循环
                break
            
        # if mark == 0:
        #     for node in qnetwork.nodeList:
        #         if node.epsFlag == 1:
        #             print(f"eps {node.nodeID} with the selectNode:")
        #             print(node.selectNodeID)
        #     # print(qnetwork.elMatrix)

        if if_change == True:
            print("本时隙未完成收敛")
            # qnetwork.elMatrix[qnetwork.elMatrix.shape[0] - 1] = qnetwork.ec_selectNodes[qnetwork.elMatrix.shape[0] - 1]
            ec_selectNodes = qnetwork.ec_selectNodes
            max_matrix = tuple_to_matrix(max(ec_selectNodes, key=ec_selectNodes.get))
            qnetwork.elMatrix[qnetwork.elMatrix.shape[0] - 1] = max_matrix[qnetwork.elMatrix.shape[0] - 1]

        qnetwork.update_elMatrix()
        # print("完成update",qnetwork.elMatrix)
        qnetwork.check_if_ec_and_update()
        # print("完成ec check",qnetwork.elMatrix)
        qnetwork.elMatrix = sliding_matrix(qnetwork.elMatrix)
        # print("qnetwork.ec_counter:", qnetwork.ec_counter)
        # if mark == 0:
        #     print(qnetwork.elMatrix)
            # print("完成滑动",qnetwork.elMatrix)

    # print(f"共建成ec数量为{qnetwork.ec_counter}")
    ec_counter_average += qnetwork.ec_counter
    # print(f"end with{qnetwork.elMatrix}")

print("best_response")
print(f"平均每次测试建成ec数量为{ec_counter_average/loop_num}")
end_time = time.time()

total_time = end_time - start_time

print(f"程序执行总时间为: {total_time} 秒")

# # qnetwork = QNetwork("./data/generate.txt", 5, 10)
# # epList = qnetwork.get_epList() 
# # print("epList",epList)
# # print("sd_pair list",qnetwork.sdList)
# # eps_ep_index, eps_ep_nodes = qnetwork.get_eps_ep_list(epList)
# # print("eps_ep_index",eps_ep_index)
# # print("eps_ep_nodes", eps_ep_nodes)


start_time = time.time()
ec_counter_average = 0
for i in range(loop_num):
    # print(f"第{i+1}次实验")
    qnetwork = QNetwork("./data/generate.txt", remaining_num, node_num)
    # print("拓扑已生成")
    # epList = qnetwork.get_epList() 
    # eps_ep_index, eps_ep_nodes = qnetwork.get_eps_ep_list(epList)

    mark = i

    # print(f"start with{qnetwork.elMatrix}")
    # 时隙数
    # print(f"目前ec数量为{qnetwork.ec_counter}")
    for i in range(time_num):
        qnetwork.Step_first()

        # if mark == 0:
        #     for node in qnetwork.nodeList:
        #         if node.epsFlag == 1:
        #             print(f"eps {node.nodeID} with the selectNode:")
        #             print(node.selectNodeID)
        #     print(qnetwork.elMatrix)
        # qnetwork.update_elMatrix()
        # # print("完成update",qnetwork.elMatrix)
        # qnetwork.check_if_ec_and_update()
        # # print("完成ec check",qnetwork.elMatrix)
        # qnetwork.elMatrix = sliding_matrix(qnetwork.elMatrix)
        # # print("完成滑动",qnetwork.elMatrix)

    # print(f"共建成ec数量为{qnetwork.ec_counter}")
    ec_counter_average += qnetwork.ec_counter
    # print(f"end with{qnetwork.elMatrix}")

print("step_first")
print(f"平均每次测试建成ec数量为{ec_counter_average/loop_num}")


end_time = time.time()

total_time = end_time - start_time

print(f"程序执行总时间为: {total_time} 秒")


start_time = time.time()
ec_counter_average = 0
for i in range(loop_num):
    # print(f"第{i+1}次实验")
    qnetwork = QNetwork("./data/generate.txt", 5, node_num)
    # print("拓扑已生成")
    # epList = qnetwork.get_epList() 
    # eps_ep_index, eps_ep_nodes = qnetwork.get_eps_ep_list(epList)

    mark = i
    # print(f"start with{qnetwork.elMatrix}")
    # 时隙数
    # print(f"目前ec数量为{qnetwork.ec_counter}")
    for i in range(time_num):
        qnetwork.Remainning_first()

        # if mark == 0:
        #     for node in qnetwork.nodeList:
        #         if node.epsFlag == 1:
        #             print(f"eps {node.nodeID} with the selectNode:")
        #             print(node.selectNodeID)
        #     print(qnetwork.elMatrix)
        # qnetwork.update_elMatrix()
        # # print("完成update",qnetwork.elMatrix)
        # qnetwork.check_if_ec_and_update()
        # # print("完成ec check",qnetwork.elMatrix)
        # qnetwork.elMatrix = sliding_matrix(qnetwork.elMatrix)
        # # print("完成滑动",qnetwork.elMatrix)

    # print(f"共建成ec数量为{qnetwork.ec_counter}")
    ec_counter_average += qnetwork.ec_counter
    # print(f"end with{qnetwork.elMatrix}")

print("remaining_first")
print(f"平均每次测试建成ec数量为{ec_counter_average/loop_num}")


end_time = time.time()

total_time = end_time - start_time

print(f"程序执行总时间为: {total_time} 秒")


# ec_counter_average = 0
# for i in range(loop_num):
#     print(f"第{i+1}次实验")
#     qnetwork = QNetwork("./data/generate.txt", remaining_num, node_num)
#     # print("拓扑已生成")

#     mark = i
#     # print(f"start with{qnetwork.elMatrix}")
#     # 时隙数
#     for i in range(time_num):

#         print("开始循环")

#         if_change = False  # 假设 False 表示没有变化
#         for i in range(10):
#             if_change = qnetwork.old_updateSelectNodeID()

#             # print(if_change)
#             if not if_change:  # 如果 if_change 为 False，则退出内层循环
#                 break

#         if mark == 0:
#             for node in qnetwork.nodeList:
#                 if node.epsFlag == 1:
#                     print(f"eps {node.nodeID} with the selectNode:")
#                     print(node.selectNodeID)
#             print(qnetwork.elMatrix)



                
#             # print(qnetwork.elMatrix)
 
#         # print("循环结束")
#         # print(qnetwork.elMatrix)

#         if if_change == False:

#             qnetwork.update_elMatrix()
#             # print("完成update",qnetwork.elMatrix)
#             qnetwork.check_if_ec_and_update()
#             # print("完成ec check",qnetwork.elMatrix)
#             qnetwork.elMatrix = sliding_matrix(qnetwork.elMatrix)
#             print("qnetwork.ec_counter:", qnetwork.ec_counter)

#         # if mark == 0:
#         #     print(qnetwork.elMatrix)
#             # print("完成滑动",qnetwork.elMatrix)

#     # print(f"共建成ec数量为{qnetwork.ec_counter}")
#     ec_counter_average += qnetwork.ec_counter
#     # print(f"end with{qnetwork.elMatrix}")

# print("old_best_response")
# print(f"平均每次测试建成ec数量为{ec_counter_average/loop_num}")
# end_time = time.time()

# total_time = end_time - start_time

# print(f"程序执行总时间为: {total_time} 秒")





# qnetwork = QNetwork("./data/generate.txt", remaining_num, node_num)
# epList = qnetwork.get_epList() 
# print("epList",epList)
# print("sd_pair list",qnetwork.sdList)
# eps_ep_index, eps_ep_nodes = qnetwork.get_eps_ep_list(epList)
# print("eps_ep_index",eps_ep_index)
# print("eps_ep_nodes", eps_ep_nodes)